// pages/addlist/addlist.js
const requests = require('../../../config/request');

var eventChannel
Page({

    /**
     * 页面的初始数据
     */
    data: {
        addressList: [],
        from: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        eventChannel = this.getOpenerEventChannel()
        if (options.from) {
            this.setData({
                from: options.from
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.addressList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    addressList: function () {
        requests.addressList()
            .then(({ list }) => {
                this.setData({
                    addressList: list
                })
        })
    },
    btnAddressEdit: function(e) {
        let data = e.currentTarget.dataset;
        wx.navigateTo({
            url: './../../userAddress/addressEdit/addressEdit?address_id=' + data.address_id 
            + "&from=" + this.data.from
        })
    },
    addressSet: function(e) {
        let data = e.currentTarget.dataset;
        requests.addressSet(data)
            .then(({ result }) => {
                this.addressList()
        })
    },
    addressSelect: function(e) {
        let data = e.currentTarget.dataset;
        eventChannel.emit('selected', { address: data.address });
        wx.navigateBack({
          delta: 0,
        })
    },
})